////////////////////////////////////////////////////////////////////////////////
//      
//      Kingsoft File for shp routines file
//      
//      File      : shp_txt.h
//      Comment   : simple html parser txt obj class
//					
//      Create at : 2009-04-24
//      Create by : chenguicheng
//      
////////////////////////////////////////////////////////////////////////////////

#pragma once

#define SHP_TXT_ITEM_KEY					TEXT("txt")

#include "shpsupport.h"

class shp_txt_item : public shp_dom_item
{
public:

	shp_txt_item()
	:m_clr_text(CLR_INVALID),
	m_u_fmt(0),
	m_w_font(0)
	{};

	~shp_txt_item()
	{};

	//set_attribute( p, m )
	BEGIN_KEY_MAP()
		//to add new attribute map( key map to member )
		ADD_CLR_KEY(  L"clr_text",	m_clr_text )
		ADD_FONT_KEY( L"font",		m_w_font )
		ADD_HEX_KEY(  L"fmt",		m_u_fmt )
		BASE_CLASS_PROCESS(shp_dom_item)
	END_KEY_MAP()

	void init_rect( CRect& parent_rect )
	{
		__super::calc_rect( parent_rect );
	}

	void draw_item( shpdc& dc, CRect& draw_rect )
	{
		CRect rect = get_item_rect();
		_draw_item( dc, rect, true );
		set_item_rect( rect );
	}

	bool hit_test( UINT umsg, int x, int y, DWORD& dw_id )
	{
		bool  b_ret = false;
		switch( umsg )
		{
		case WM_MOUSEMOVE:
			b_ret = _mouse_over( x, y );
			break;
		case WM_MOUSELEAVE:
			b_ret = _mouse_leave( x, y );
			break;
		case WM_LBUTTONDOWN:
			b_ret = _lbutton_down( x, y );
			break;
		case WM_LBUTTONUP:
			b_ret = _lbutton_up( x, y );
			break;
		}
		
		return b_ret;
	}

	void _draw_item( shpdc& dc, CRect& rect, bool b_calc )
	{
		UINT	u_fmt = m_u_fmt;
		shpclr	clr_text = m_clr_text;
		WORD	w_font = m_w_font;
		shp_style* p_style = shp_style_mgr::Instance().get_style( get_str_style() );

		if( p_style )
			p_style->get_state_style( clr_text, w_font, get_state() );

		if( p_style && u_fmt == 0 )
			u_fmt = p_style->m_u_fmt;

		shpfont font;
		if( w_font == 0 )
			font = shp_font_mgr::Instance().get_def_font( );
		else
			font = shp_font_mgr::Instance().get_font( w_font );

		if( p_style )
		{
			rect.left += p_style->m_x_margin;
			rect.top += p_style->m_y_margin;
		}

		shp_draw::draw_text( dc, get_str_text(), rect, u_fmt, font, clr_text, b_calc );		
	}


protected:

	bool _mouse_over( int x, int y )
	{
		if( get_state() == item_state_disable )
			return false;

		if( get_state() != item_state_hover )
		{
			set_state( item_state_hover );
			return true;
		}

		return false;
	}

	bool _mouse_leave( int x, int y )
	{
		if( get_state() == item_state_disable )
			return false;

		if( get_state() != item_state_normal )
		{
			set_state( item_state_normal );
			return true;
		}

		return false;
	}

	bool _lbutton_down( int x, int y )
	{
		if( get_state() == item_state_disable )
			return false;

		if( get_state() != item_state_down )
		{
			set_state( item_state_down );
			return true;
		}

		return false;
	}

	bool _lbutton_up( int x, int y )
	{
		if( get_state() == item_state_disable )
			return false;

		if( get_state() != item_state_hover )
		{
			set_state( item_state_hover );
			return true;
		}

		return false;
	}
	


private:
	
	shpclr		m_clr_text;
	UINT		m_u_fmt;
	WORD		m_w_font;

};